package com.spring.boot.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.boot.entity.*;
import com.spring.boot.response.Result;
import com.spring.boot.service.*;
import com.spring.boot.utils.MyUtils;
import com.spring.boot.vo.CityVo;
import com.spring.boot.vo.RadarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@RestController
@PreAuthorize("hasAnyAuthority('ROLE_bigScreen')")
@RequestMapping("/bigScreen")
public class BigScreenController {

    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private RankingService rankingService;
    @Autowired
    private RadarService radarService;
    @Autowired
    private ProvinceMapService provinceMapService;
    @Autowired
    private CityMapService cityMapService;
    @Autowired
    private PredictionService predictionService;
    @Autowired
    private InOutNumService inOutNumService;
    @Autowired
    private DangerLevelService dangerLevelService;
    @Autowired
    private ChinaCityService chinaCityService;
    @Autowired
    private SummaryService summaryService;

    @GetMapping("/province")
    public Result<?> getProvinceInfo(@PathParam("date") String date) {
        List<TitleDate> result = provinceService.listTitleDate(date);
        if (ObjectUtil.isNull(result)) {
            return Result.error("数据概览查询为空！");
        } else {
            return Result.OK(result);
        }
    }

    @GetMapping("/city")
    public Result<?> getCityInfo() {
        CityVo result = cityService.getCityInfo(new QueryWrapper<>());
        if (ObjectUtil.isNull(result)){
            return Result.error("城市统计数据查询为空！");
        } else {
            return Result.OK(result);
        }
    }
    @GetMapping("/ranking")
    public Result<?> getRankingInfo(@PathParam("date") String date){
        List<?> result = chinaCityService.getRankingInfo(date);
        if (ObjectUtil.isNull(result)){
            return Result.error("新增排名查询为空！");
        } else {
            return Result.OK(result);
        }
    }
    @GetMapping("/radarData")
    public Result<?> getRadarData(@PathParam("date") String date){
        if (StrUtil.hasEmpty(date)){
            date = "";
        }
        RadarVo result = radarService.getRadarData(date);
        if (ObjectUtil.isNull(result)){
            return Result.error("雷达图查询为空！");
        } else {
            return Result.OK(result);
        }
    }
    @GetMapping("/provinceMapData")
    public Result<?> getProvinceMapData(@PathParam("date") String date){
        List<Integer> result = chinaCityService.getProvinceMapData(date);
        if (ObjectUtil.isNull(result)){
            return Result.error("市级数据查询为空！");
        } else {
            return Result.OK(result);
        }
    }

    //区县级地图数据，目前废弃
    @GetMapping("/cityMapData")
    public Result<?> getCityMapData() {
        List<Integer> result = cityMapService.getCityMapData();
        if (ObjectUtil.isNull(result)){
            return Result.error("区县级地图数据查询为空！");
        } else {
            return Result.OK(result);
        }
    }

    @GetMapping("/predictionData")
    public Result<?> getPredictionData() {
        Map result = predictionService.getPredictionData();
        if (ObjectUtil.isNull(result)){
            return Result.error("预测参数查询为空！");
        } else {
            return Result.OK(result);
        }
    }

    @GetMapping("/dangerLevel")
    public Result<?> getDangerLevel(@PathParam("date") String date) {
        if (StrUtil.hasEmpty(date)){
            date = MyUtils.today();
        }
        DangerLevel result = dangerLevelService.getOne(new QueryWrapper<DangerLevel>().likeRight("date", date));
        if (ObjectUtil.isNull(result)){
            return Result.error("危险等级查询为空！");
        } else {
            return Result.OK(result);
        }

    }

    @GetMapping("/flyLineData")
    public Result<?> getFlyLineData(@PathParam("date") String date) {
        if (StrUtil.hasEmpty(date)){
            date = MyUtils.getToday();//这个时间格式是yyyymmdd
        } else {
           date = date.replace("-","");
        }
        List<InOutNum> result = inOutNumService.getFlyLineData(date);
        if (ObjectUtil.isNull(result)) {
            return Result.error("飞线数据查询为空！");
        } else {
            return Result.OK(result);
        }

    }

    @GetMapping("/summary")
    public Result<?> getSummary(@PathParam("date") String date) {
        if (StrUtil.hasEmpty(date)) {
            date = MyUtils.today();
        }
        List<Summary> result = summaryService.list(new QueryWrapper<Summary>().eq("create_time", date));

        if (result.size() == 0) {
            return Result.error("总结数据查询为空！");
        } else {
            return Result.OK(result);
        }

    }


    @GetMapping("/citiesTrend")
    public Result<?> getCitiesTrend() {
//        Map result = cityService.getCitiesTrend();
        Map result = chinaCityService.getCitiesTrend();
        return Result.OK(result);
    }

    //预测参数接口，目前废弃
    @GetMapping("/predictionData/page")
    public Result<?> pagePredictionData(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(name = "search", defaultValue = "") String search) {
        Page<PredictParam> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PredictParam> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(search)) {
            queryWrapper.like("name", search);
        }
        Page<PredictParam> result  =  predictionService.page(page, queryWrapper);
        return Result.OK(result);
    }





}
